#!/user/bin/env python3
# -*- coding: utf-8 -*-
import json
from loginfo import Logger

logger = Logger()


def conv(url, old_data):
    try:
        # 解析JSON串
        data = json.loads(json.dumps(old_data))
        # 修改alertname字段
        for alert in data['alerts']:
            if 'instance' in alert['labels']:  # 当instance 存在，表示为节点类预警
                alert['labels']['alertname'] = alert['labels']['alertname'] + "-" + alert['labels']['instance']
            else:
                alert['labels']['alertname'] = alert['labels']['alertname'] + "-" + alert['labels']['pod']
        # 生成新的JSON串
        new_json_str = json.dumps(data, ensure_ascii=False).encode('utf-8').decode('utf-8')
        # 打印新的JSON串
        print(new_json_str)
        logger.loginfo("New-webhook-json: " + new_json_str)
        print("将接受的url、msg 进行转发")
    except Exception as e:
        # 捕捉异常，并进行相应的处理
        print("Exception:", str(e))
        logger.loginfo("conv-error: " + str(e) + "")
